{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 获得目标和的连续子序列\n",
    "\n",
    "[![Author](https://img.shields.io/badge/Author-%40mjd507-important?style=social&logo=GitHub)](https://github.com/mjd507)\n",
    "[![WeChat Channel](https://img.shields.io/badge/Wechat-Daily%20Problem-important?style=social&logo=WeChat)](https://mp.weixin.qq.com/s/V-W3T-_3iI8_wkqSbi-5cw)\n",
    "\n",
    "![Logo](img/Day87-1.png)\n",
    "\n",
    "&emsp;&emsp;给定一个整数数组和一个整数$K$，返回总和为$K$的连续子序列。你可以认为解始终存在。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_continuous_k(array: list, k: int) -> list:\n",
    "    dictionary = {}\n",
    "    summary = 0\n",
    "    dictionary[0] = -1\n",
    "    for i in range(len(array)):\n",
    "        item = array[i]\n",
    "        summary += item\n",
    "        dictionary[summary] = i\n",
    "        try:\n",
    "            firstIndex = dictionary[summary - k]\n",
    "        except KeyError:\n",
    "            firstIndex = None\n",
    "        if firstIndex is not None:\n",
    "            return array[firstIndex + 1: i + 1]\n",
    "    return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 5, 7]\n"
     ]
    }
   ],
   "source": [
    "print(find_continuous_k([1, 3, 2, 5, 7, 2], 14))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
